package org.dawn.deveco.plugin.resourceeditor.model;

import org.dawn.deveco.plugin.resourceeditor.common.Constants;
import org.dawn.deveco.plugin.resourceeditor.model.item.BaseItem;
import org.dawn.deveco.plugin.resourceeditor.model.item.MediaItem;
import org.dawn.deveco.plugin.resourceeditor.utils.Logger;

import javax.swing.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;

public class MediaResourceContent extends BaseResourceContent {
    public static final int ICON_COLUMN_INDEX = 3;
    public static final int FULL_FILENAME_COLUMN_INDEX = 2;

    private static final Logger LOG = Logger.getInstance(MediaResourceContent.class);

    public MediaResourceContent() {
        setCategory(Constants.MEDIA_CATEGORY);
        setHeader("Name", "Filename", "FullFilename", "", "");
        setColumnClasses(new Class[]{String.class, String.class, String.class, Icon.class, Icon.class});
    }

    /**
     * 返回 DefaultTableModel 的数据
     * @param group 在 Media 类型数据中，由于只有一个media文件夹，group 被忽略
     * @return
     */
    public Vector<Vector> getJTableContent(String group) {
        List<? extends BaseItem> itemList = this.getGroupData(group);
        Vector<Vector> list = new Vector<>();
        for (final BaseItem e : itemList){
            MediaItem item = (MediaItem) e;
            Vector<Object> vec = new Vector<>(3);
            vec.add(item.name);
            vec.add(item.fileName);
            vec.add(item.fullFileName);
            vec.add(null);   // Icon 列
            vec.add(null);   // Action 列
            list.add(vec);
        }
        return list;
    }

    public boolean save() {
        boolean ret = false;

        String path = Constants.RESOURCE_PATH_MAP.get(Constants.MEDIA_CATEGORY);
        Path mediaDir = Paths.get(Constants.RESOURCE_ROOT_PATH, path);

        Vector<Vector> data = this.tableDataByGroup.get(MediaResourceContent.DEFAULT_GROUP);

        try {
            List<Path> existedFileList = Files.list(mediaDir).filter(Files::isRegularFile)
                    .collect(Collectors.toList());
            List<Path> mediaList = data.stream().filter(item -> !item.isEmpty())
                    .map(item -> Paths.get((String) item.get(MediaResourceContent.FULL_FILENAME_COLUMN_INDEX)))
                    .collect(Collectors.toList());
            List<Path> newList = new ArrayList<>(mediaList);
            // 复制新增的 media 文件到 media 目录
            newList.removeAll(existedFileList);
//            LOG.debug("newList: " + newList);
            for (Path mediaPath : newList) {
                Path newFilePath = Paths.get(mediaDir.toString(), mediaPath.getFileName().toString());
                Files.deleteIfExists(newFilePath);
                Files.copy(mediaPath, newFilePath);
            }
            // 删除已删除的 media 文件
            existedFileList.removeAll(mediaList);
//            LOG.debug("removedList: " + existedFileList);
            for (Path mediaPath : existedFileList) {
                Files.deleteIfExists(mediaPath);
            }
            ret = true;
        } catch (Exception e) {
            LOG.error("failed to save resource file to dir: " + mediaDir, e);
        }

        if (ret) {
            setChanged(false);
        }
        return ret;
    }

}
